<?

class Display
{
	private $theme;
	public $config;
	private $widgets;	
	
	public function __get($var)	{	return get_instance()->$var; }
	public function __construct()
	{
		$this->widgets = array();
		$this->config = array();
		$this->load->spark('twiggy/0.8.5');	
		//$this->set_theme('cms');
	}
		
	public function get_theme()
	{
		return $this->theme;
	}
	
	public function set_theme($theme)
	{
        
		$this->theme = $theme;
		$this->twiggy->theme($theme);
		$this->config = $this->get_theme_config($theme);

		if($this->config['template_helper'] == TRUE)
		{
			require_once(FCPATH . 'themes/'.$theme.'/template_helper.php');
		}
	}
	public function get_config($item = FALSE)
	{
		print_r($this->config);
		if($item)
			return $this->config[$item];
		else
			return $this->config;
	}

	public function get_theme_config($theme)
	{
		@include(FCPATH . 'themes/'.$theme.'/config.php');
		if(isset($config))
			return $config;
		else
			return FALSE;		
	}
	public function array_json($array)
	{
		$this->json(json_encode($array));		
	}
	
	public function json($json)
	{
		$this->set_headers_no_cache();
		$this->output->set_content_type('application/json');
		$this->output->set_output($json);		
	}
	public function json_success($array)
	{
		$this->array_json(array_merge(array('success' => true), $array));
	}
	public function json_error($message = 'error')
	{
		$this->array_json(array('success' => false, 'message' => $message));
	}
	
	public function get_layout_widget_areas($layout_name)
	{
		$theme_config = $this->display->get_theme_config('site');
		$output = array();

		foreach($theme_config['page_layouts'] as $layout)
		{
			if($layout['name'] == $layout_name)
			{
				foreach($layout['widget_areas'] as $area)
				{
					$output[] = $area;			
				}
			}
		}
		return empty($output) ? FALSE : $output;		
	}	
	
	public function template($file, $data = array())
	{
	    //echo $file;	
	    //echo $this->theme;
		// Set default template variables
		$this->twiggy->set('stylesheet_path', base_url() . 'themes/' . $this->theme . '/css/');
		$this->twiggy->set('javascript_path', base_url() . 'themes/' . $this->theme . '/js/');
		$this->twiggy->set('image_path', base_url() . 'themes/' . $this->theme . '/img/');
		$this->twiggy->set('cms_uri', site_url(config_item('cms_uri')));
		
		// CSRF Security
		$this->twiggy->set('csrf_token', $this->security->get_csrf_token_name());
		$this->twiggy->set('csrf_hash', $this->security->get_csrf_hash());
		
		// Allowed function calls
		$this->twiggy->register_function('lang');
		$this->twiggy->register_function('config_item');
		$this->twiggy->register_function('print_r');
		$this->twiggy->register_function('site_url');
		$this->twiggy->register_function('permission');
		$this->twiggy->register_function('cms_url');
		$this->twiggy->register_function('json_encode');
		
		// From template helper
		if($this->config['template_helper'] == TRUE && !empty($this->config['register_functions']))
		{
			foreach($this->config['register_functions'] as $func)
			{
				$this->twiggy->register_function($func);
			}
		}
		
		// Flash message
		if($this->session->flashdata('message'))
		{
			$this->twiggy->set('message', $this->session->flashdata('message'));
			$this->twiggy->set('message_type', $this->session->flashdata('message_type'));			
		}
		
		// Meta data
		$title = $this->theme == 'cms' ? config_item('cms_name') : config_item('site_name');
		$author = $this->theme == 'cms' ? config_item('cms_author') : config_item('site_author');
		$description = $this->theme == 'cms' ? config_item('cms_description') : config_item('seo_description');

		$this->twiggy->title($title);
		$this->twiggy->meta('description', $description);
		$this->twiggy->meta('author', $author);	

		// User data
		$this->twiggy->set('logged_in', $this->ion_auth->logged_in());
		$this->twiggy->set('is_admin', $this->ion_auth->is_admin());
		if( $this->ion_auth->logged_in() )
		{
			$this->twiggy->set('user', config_item('user'));
			$this->twiggy->set('group', config_item('group'));			
		}
		
		// Widgets
		if( ! empty($this->widgets) )
		{
			foreach($this->widgets as $widget)
			{
				$this->twiggy->set($widget['area_name'], Widget::run($widget['name'], TRUE));
			}
			
		}
		
		// Load passed variables into the template
		foreach($data as $key => $value)
			$this->twiggy->set($key, $value);	
        
		// Save rendered template html into output variable
		$output = $this->twiggy->template($file)->render();		
		
		// Set output headers to no caching
		$this->set_headers_no_cache();
		
		// Display the output 
		$this->output->set_output($output);
	}
	
	private function get_page_config($name)
	{
				
	}
	
	private function set_headers_no_cache()
	{
		$this->output->set_header("HTTP/1.1 200 OK");
		//$this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
		$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
		$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
		$this->output->set_header("Pragma: no-cache"); 	
	}
	
	public function get_menu($theme)
	{
		
	}
}